<?php
session_start();
include "../include/dbFunctions.php";
include "../include/formFunctions.php";
include "../include/menu.php";
$eid = $_SESSION['eid'];
$queryt = "SELECT * FROM session WHERE event_id = $eid";
$session_array = executeSelectQuery($queryt);

//session detail
//first-last date of event
$first = $last = $session_array[0]['session_date'];
for ($i = 0; $i < count($session_array); $i++) {
    if ($session_array[$i]['session_date'] < $first) {
        $first = $session_array[$i]['session_date'];
    }
    if ($session_array[$i]['session_date'] > $last) {
        $last = $session_array[$i]['session_date'];
    }
}
$querys = "SELECT *
            FROM school s, event e, location l, education ed
            WHERE s.education_id = ed.education_id
            AND e.school_id = s.school_id
            AND s.location_id = l.location_id
            AND e.event_id =$eid";
$school_array = executeSelectQuery($querys);
//School detail
$education_id = $school_array[0]['education_id'];
$location_id = $school_array[0]['location_id'];
//
//
//
$queryv = "SELECT * FROM volunteer
WHERE volunteer_status ='active'";
$volunteer_array1 = executeSelectQuery($queryv);
for ($i = 0; $i < count($volunteer_array1); $i++) {
    $vid = $volunteer_id = $volunteer_array1[$i]['volunteer_id'];
//
    $querysp = "SELECT * FROM time_preference p, time t WHERE t.time_id = p.time_id AND volunteer_id = $vid";
    $session_preference = executeSelectQuery($querysp);
//
//
//
//
//Only for detail
    $volunteer_array1[$i]['sessions'] = $volunteer_array1[$i]['edus'] = $volunteer_array1[$i]['locations'] = $volunteer_array1[$i]['ongoings'] = 0;
    $query11 = "SELECT DISTINCT(education_name) FROM education_preference, education WHERE volunteer_id= $vid";
    $edu_pre = executeSelectQuery($query11);
    $query12 = "SELECT DISTINCT(location_name) FROM location_preference, location WHERE volunteer_id= $vid";
    $loc_pre = executeSelectQuery($query12);

//match1: session match
    //check trainer status
    $query20 = "SELECT * FROM session WHERE volunteer_id= $vid";
    $ongoing_session = executeSelectQuery($query20);
    $volunteer_array1[$i]['status'] = 'none';
    $query5 = "SELECT * FROM trainer WHERE volunteer_id = '" . $vid . "' AND event_id = '" . $eid . "'";
    $trainer = executeSelectQuery($query5);
    if ($trainer != null) {
        $volunteer_array1[$i]['status'] = $trainer[0]['trainer_status'];
    } else {
        $volunteer_array1[$i]['status'] = "none";
    }
    //now match
    $weekday = Array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday');
    $result = 0;
    for ($y = 0; $y < count($session_array); $y++) {
        $match = false;
        $session_array[$y]['m'] = "not";
        $session_array[$y]['ce'] = "no";
        $session_array[$y]['dw'] = "Weekend";
        //get weekday/weekend
        $date = $session_array[$y]['session_date'];
        $session_date = explode('-', $date);
        $day = $session_date[2];
        $month = $session_date[1];
        $year = $session_date[0];
        $session_array[$y]['day'] = date("l", mktime(0, 0, 0, $month, $day, $year));
        //check if it's weekday/weekend
        $day_id = 1;
        for ($j = 0; $j < count($weekday); $j++) {
            if ($weekday[$j] == $session_array[$y]['day']) {
                $day_id = 0;
                $session_array[$y]['dw'] = "Weekday";
            }
        }
        //check if it's match
        for ($l = 0; $l < count($session_preference); $l++) {
            if ($day_id == $session_preference[$l]['time_id']) {
                //now check if the match is on current event
                $match = true;
            }
            if ($match == true) {
                for ($k = 0; $k < count($ongoing_session); $k++) {
                    if ($date == $ongoing_session[$k]['session_date']) {
                        $session_array[$y]['ce'] = "yes";
                        $match = false;
                    }
                }
            }
        }
        $session_array[$y]['match'] = $match;
        if ($session_array[$y]['match'] == true) {
            $result += 69 / (count($session_array));
            $session_array[$y]['m'] = "match";
            $volunteer_array1[$i]['sessions'] = $result;
        }
    }
    if ($result == 69) {
        $volunteer_array1[$i]['session'] = "match";
    } elseif ($result == 0) {
        $volunteer_array1[$i]['session'] = "not";
    } else {
        $volunteer_array1[$i]['session'] = "partial";
    }
    $volunteer_array1[$i]['match'] = $result;
//match2: educational preference
    $query2 = "SELECT * FROM education_preference
                    WHERE education_id = $education_id
                    AND volunteer_id = $vid";
    $result2 = executeSelectQuery($query2);
    if ($result2 != null) {
        $volunteer_array1[$i]['education'] = "match";
        $volunteer_array1[$i]['match'] += 10;
        $volunteer_array1[$i]['edus'] = 10;
    } else {
        $volunteer_array1[$i]['education'] = "not";
    }
//match3: location
    $query3 = "SELECT * FROM location_preference
                    WHERE location_id = $location_id
                    AND volunteer_id = $vid";
    $result3 = executeSelectQuery($query3);
    if ($result3 != null) {
        $volunteer_array1[$i]['location'] = "match";
        $volunteer_array1[$i]['match'] += 10;
        $volunteer_array1[$i]['locations'] = 10;
    } else {
        $volunteer_array1[$i]['location'] = "not";
    }
//match 4: ongoing\ REDOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
    $queryoe = "SELECT *
                    FROM event e, trainer t
                    WHERE e.event_id = t.event_id
                    AND t.volunteer_id = $vid
                    AND t.trainer_status= 'confirmed'
                    AND e.event_id != $eid";
    $ongoing_event = executeSelectQuery($queryoe);
    $volunteer_array1[$i]['ongoing'] = "match";
    if ($ongoing_event != null) {
        for ($k = 0; $k < count($ongoing_event); $k++) {
            $query51 = "SELECT * FROM session WHERE event_id = " . $ongoing_event[$k]['event_id'];
            $session_ongoing = executeSelectQuery($query51);
            $ongoing_event[$k]['first'] = $ongoing_event[$k]['last'] = $session_ongoing[0]['session_date'];
            for ($m = 0; $m < count($session_ongoing); $m++) {
                if ($ongoing_event[$k]['first'] > $session_ongoing[$m]['session_date']) {
                    $ongoing_event[$k]['first'] = $session_ongoing[$m]['session_date'];
                }
                if ($ongoing_event[$k]['last'] < $session_ongoing[$m]['session_date']) {
                    $ongoing_event[$k]['last'] = $session_ongoing[$m]['session_date'];
                }
            }
            $ongoing_event[$k]['dup'] = 0;
        }
        for ($k = 0; $k < count($ongoing_event); $k++) {
            if (((($first < $ongoing_event[$k]['last']) && ($first > $ongoing_event[$k]['first']) )) || (
                    (($last < $ongoing_event[$k]['last']) && ($last > $ongoing_event[$k]['first'])))) {
                $ongoing_event[$k]['dup'] = 1;
                $volunteer_array1[$i]['ongoing'] = "not";
            }
        }
    }
    if ($volunteer_array1[$i]['ongoing'] == "match") {
        $volunteer_array1[$i]['match'] += 10;
        $volunteer_array1[$i]['ongoings'] = 10;
    }
}
$volunteer_array = asort2d($volunteer_array1, "match", true);
$mes = $_SESSION['mes'];
$_SESSION['mes'] = "";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>JA Scheduling</title>

        <script type="text/javascript" language="javascript" src="../media/js/jquery/jquery-1.6.1.min.js"></script>
        <script type="text/javascript" language="javascript" src="../media/js/jquery/dataTable.js"></script>

        <!--  CSS script -->
        <link rel="stylesheet" href="../media/css/screen.css" type="text/css" media="screen" title="default" />
        <link type="text/css" href="../media/css/dataTable.css" rel="stylesheet" />

        <!-- Manage table script -->
        <script type="text/javascript" charset="utf-8">
            $(document).ready(function() {
                $('#admintable').dataTable( {
                    "sPaginationType": "full_numbers"
                } );
            } );
        </script>
        <script language="JavaScript">
            function toggle1(source) { //for select all checkbox :)
                checkboxes = document.getElementsByName('id[]');
                for each(var checkbox in checkboxes)
                checkbox.checked = source.checked;
            }
        </script>

    </head>
    <body>
        <!-- DON'T TOUCH! Start: page-top-outer -->
<?php
top_outer();
?>
        <!-- End: page-top-outer -->

        <div class="clear">&nbsp;</div>

        <!--  start nav-outer-repeat................................................................................................. START -->
        <div class="nav-outer-repeat">
            <!--  start nav-outer -->
            <div class="nav-outer">

                <!-- start nav-right -->
<?php
nav_right();
?>
                <!-- end nav-right -->


                <!--  start nav -->
                <div class="nav">
                    <div class="table">

<?php
navigation_event();
?>

                        <div class="clear"></div>
                    </div>
                    <div class="clear"></div>
                </div>
                <!--  start nav -->

            </div>
            <div class="clear"></div>
            <!--  start nav-outer -->
        </div>
        <!--  start nav-outer-repeat................................................... END -->

        <div class="clear"></div>

        <!-- start content-outer ........................................................................................................................START -->
        <div id="content-outer">
            <!-- start content -->
            <div id="content">

                <!--  start page-heading --> <!--lyedit-->
                <div id="page-heading">
                    <h1>Get Volunteer for this course</h1>
                </div>
                <!-- end page-heading -->
                <table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
                    <tr>
                        <th rowspan="3" class="sized"><img src="../media/images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
                        <th class="topleft"></th>
                        <td id="tbl-border-top">&nbsp;</td>
                        <th class="topright"></th>
                        <th rowspan="3" class="sized"><img src="../media/images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
                    </tr>
                    <tr>
                        <td id="tbl-border-left"></td>
                        <td>
                            <!--  start content-table-inner ...................................................................... START -->
                            <div id="content-table-inner">
                                <h2></h2>
                                <!--  start table-content  -->

                                <div id="table-content">
                                    <!--  Can put table start from here within this div--> <!--lyedit-->

                                    <b><font color =red><?php echo $mes ?></font></b>
                                    <form action="action/eventSendRequest.php" method="post">
                                        <input type="hidden" name ="event_id" value= <?php echo $eid ?>></input>
                                        <table cellpadding="0" cellspacing="0" border="0" class="display" id="admintable">
                                            <thead>
                                                <th><input type="checkbox" onClick="toggle1(this)" /></th>
                                                <th>Salutation</th>
                                                <th>Volunteer Name</th>
                                                <th>Email</th>
                                                <th>Mobile Number</th>
                                                <th>Match</th>
                                                <th>Datetime</th>
                                                <th>Educational Preference</th>
                                                <th>Location</th>
                                                <th>Not on ongoing event </th>
                                                <th>Volunteer Status</th>
                                            </thead>
                                            <tbody>
<?php
for ($i = 0; $i < count($volunteer_array); $i++) {
    $no = $i + 1;
    $salutation = $volunteer_array[$i]['volunteer_salutation'];
    $name = $volunteer_array[$i]['volunteer_firstname'] . " " . $volunteer_array[$i]['volunteer_surname'];
    $email = $volunteer_array[$i]['volunteer_email'];
    $mobile = $volunteer_array[$i]['volunteer_mobile'];
    $match = $volunteer_array[$i]['match'];
    $timeslot = getMatchStatus($volunteer_array[$i]['session'], $volunteer_array[$i]['volunteer_id']);
    $education = getMatchStatus($volunteer_array[$i]['education'], $volunteer_array[$i]['volunteer_id']);
    $location = getMatchStatus($volunteer_array[$i]['location'], $volunteer_array[$i]['volunteer_id']);
    $notongoing = getMatchStatus($volunteer_array[$i]['ongoing'], $volunteer_array[$i]['volunteer_id']);
    $status = getVolunteerStatus($volunteer_array[$i]['status']);
    $id = $volunteer_array[$i]['volunteer_id'];

    echo "<tr>";
    echo "<td><input type='checkbox' name='id[]' value = $id></td>";
    echo "<td>$salutation</td>";
    echo "<td><a href='../volunteer/volunteerView.php?id=" . $id . "'>$name</a></td>";
    echo "<td><a href='mailto:$email?Subject=From JA Admin:'>" . $email . "</a></td>";
    echo "<td>$mobile</td>";
    echo "<td>" . $match . "%</td>";
    echo "<td>" . $timeslot . "</td>";
    echo "<td>" . $education . "</td>";
    echo "<td>" . $location . "</td>";
    echo "<td>" . $notongoing . "</td>";
    echo "<td>" . $status . "</td>";
    echo "</tr>";
}
?>
                                            </tbody>
                                        </table>
                                        <br><br><br>
                                                    <table>
                                                        <tr>
                                                            <td><input type="submit" value="&nbsp" class="form-request" onclick='return  confirm("Request emails will be sent to selected volunteers. Do you wish to continue?")'></input></td>
                                                            <td><input class="assign-trainer" value="&nbsp"  type='submit' onclick="form.action='action/eventAssignTrainer.php'; return true;"></input></td>
                                                        </tr>
                                                    </table>
                                                    <br/>
                                                    <p class="p-instruct">Add a new volunteer <a href="../volunteer/volunteerAdd.php">here</a></p>

                                                    </form>
                                                    <br/><br/>
                                                    <input type="button" class="form-back" value="" onClick="javascript:history.go(-1)"/>
                                                    <!--  end table-content here -->
                                                    </div>
                                                    <div class="clear"></div>
                                                    </div>
                                                    <!--  end content-table-inner ............................................END  -->
                                                    </td>
                                                    <td id="tbl-border-right"></td>
                                                    </tr>
                                                    <tr>
                                                        <th class="sized bottomleft"></th>
                                                        <td id="tbl-border-bottom">&nbsp;</td>
                                                        <th class="sized bottomright"></th>
                                                    </tr>
                                                    </table>
                                                    <div class="clear">&nbsp;</div>

                                                    </div>
                                                    <!--  end content -->
                                                    <div class="clear">&nbsp;</div>
                                                    </div>
                                                    <!--  end content-outer........................................................END -->
<?php
footer();
?>
                                                    </body>
                                                    </html>